/**
 * Created by lumix on 2015/4/22.
 */
var express = require('express');
var router = express.Router();
var Promise = require('promise');
var crypto = require('crypto');
var logger = require('log4js').getLogger('login');
var commonUtils = require('./../../utils/commonUtils');
var loginService = require('../service/loginService')

//登陆
router.post('/', function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;
    if(!username||!password){
        res.json({'success':false,'msg':"参数缺失"});
        return;
    }
    //var md5 = crypto.createHash('md5');
    //md5.update(content);
    //password = md5.digest('hex');
    loginService.login(username,password).then(function(r){
        if(r){
            if(r.id&& r.bak1==0){
                res.json({'success':true,'id': r.id})
            }else{
                res.json({'success':false,'msg':'该账户已禁用','status':999});
            }
        }else{
            res.json({'success':false,'msg':'账户名或密码错误'})
        }

    },function(err){
        res.json({'success':false,'msg':'网络异常'})
    })

});

router.post('/thirdParty', function (req, res, next) {
    var username = req.body.username;
    var openid = req.body.username;
    if(req.body.openid){
        openid = req.body.openid;
    }
    if(!username||!openid){
        res.json({'success':false,'msg':"参数缺失"});
        return;
    }
    loginService.getByOpenid(openid).then(function (mems) {
        if(mems&&mems.length>0){
            return Promise.resolve(mems[0]);
        }else{
            return loginService.registerThirdParty(username,openid,'iseeu').then(function(m){
                if(m){
                    var result = {};
                    result.id = m.insertId;
                    return Promise.resolve(result);
                }else{
                    return Promise.resolve(null);
                }
            })
        }
    }).then(function (result) {
        if(result){
            res.json({'success':true,'id':result.id});
        }else{
            res.json({'success':false,'msg':"登陆失败"});
        }

    }, function (err) {
        console.log(err);
        logger.error(err);
        res.json({'success':false,'msg':"网络异常"});
    })


})

//注册
router.post('/register',function(req,res,next){
    var username = req.body.username;
    var password = req.body.password;
    if(!username||!password){
        res.json({'success':false,'msg':"参数缺失"});
        return;
    }
    //var md5 = crypto.createHash('md5');
    //md5.update(content);
    //password = md5.digest('hex');
    loginService.getByUsername(username).then(function(m){
        console.log(m)
        if(m&& m.length>0){
            return Promise.resolve(-1);
        }else{
            return loginService.register(username,password).then(function(r){
                if(r&&r.insertId){
                    var result = {};
                    result.id = r.insertId;
                    return Promise.resolve(result);
                }else{
                    return Promise.resolve(null);
                }
            })
        }
    }).then(function(result){
        if(result){
            if(-1==result){
                res.json({'success':false,'msg':'该账户已注册'});
            }else{
                res.json({'success':true,'id':result.id});
            }
        }else{
            res.json({'success':false,'msg':"注册失败"});
        }
    }, function (err) {
        console.log(err);
        logger.error(err);
        res.json({'success':false,'msg':"网络异常"});
    })

})
//发邮件
router.post("/sendMail",function(req,res,next){
    var mail = req.body.mail;
    loginService.sendMail(mail).then(function(r){
        if(r){
            res.json({"success":true,"msg":"发送成功"});
        }else{
            res.json({"success":false,"msg":"发送失败"});
        }
    },function(err){
        res.json({'success':false,'msg':'网络异常'})
    });
})
/*
获取封面图片
 */
router.get("/getCover", function (req, res, next) {
    loginService.getCover().then(function (imgUrl) {
        imgUrl = 'http://120.76.77.123:8080/images/data/upload/'+imgUrl
        res.json({"success":true,"imgUrl":imgUrl});
    })
})


module.exports = router;